package com.cf.docker.service;

import com.cf.constant.EnumReturnCode;
import com.cf.docker.model.Image;
import com.cf.docker.model.Machine;
import com.cf.docker.service.ssh.SSHResInfo;
import com.cf.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * boot-awm-DockerService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/20 下午 03:06
 */
public class DockerService {

    @Autowired
    private SSHService sshService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private MachineService machineService;


    /**
     * 拉取image
     *
     * @param image
     * @return
     */
    public boolean pullImage(long machineId, String image) {
        if (StringUtils.isEmpty(image)) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "拉取image参数为空！");
        }
        Machine machine = machineService.selectOne(machineId);
        SSHResInfo sshResInfo = sshService.sendCmd("docker pull " + image, machine.getIp(), machine.getUserName(), machine.getPassword());
        return sshResInfo.isSuccess();
    }

    /**
     * 创建容器
     *
     * @param imageId
     * @param containerParameter
     * @return
     */
    public String createDockerContainer(long imageId, String containerParameter) {
        Image image = imageService.selectOne(imageId);
        if (image == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "镜像不存在！");

        Machine machine = machineService.selectOne(image.getMachineId());
        if (machine == null) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "物理主机不存在！");
        }
        SSHResInfo sshResInfo = sshService.sendCmd("docker create  " + containerParameter + "  " + image.getPath(), machine.getIp(), machine.getUserName(), machine.getPassword());
        if (sshResInfo.isSuccess()) {
            return sshResInfo.getResult();
        } else {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "创建容器失败！");
        }
    }


    /**
     * 运行容器
     *
     * @param
     * @return
     */
    public boolean runDockerContainer(long imageId, String containerId) {
        Image image = imageService.selectOne(imageId);
        if (image == null)
            throw new BizException(EnumReturnCode.ERROR.getCode(), "镜像信息不存在！");

        Machine machine = machineService.selectOne(image.getMachineId());
        if (machine == null) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "物理主机不存在！");
        }
        SSHResInfo sshResInfo = sshService.sendCmd("docker start " + containerId, machine.getIp(), machine.getUserName(), machine.getPassword());
        if (sshResInfo.isSuccess()) {
            return true;
        } else {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "启动容器失败！" + sshResInfo.getResult());
        }
    }

    /**
     * 停止容器
     *
     * @param machineId
     * @param containerId
     */
    public void stopContainer(long machineId, String containerId) {
        Machine machine = machineService.selectOne(machineId);

        if (machine == null) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "物理主机不存在！");
        }

        SSHResInfo killResInfo = sshService.sendCmd("docker kill " + containerId, machine.getIp(), machine.getUserName(), machine.getPassword());
        if (killResInfo.isSuccess()) {
            SSHResInfo rmResInfo = sshService.sendCmd("docker rm " + containerId, machine.getIp(), machine.getUserName(), machine.getPassword());
            if (!rmResInfo.isSuccess()) {
                throw new BizException(EnumReturnCode.ERROR.getCode(), "移除容器失败！");
            }
        } else {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "关闭容器失败！");
        }
    }
}
